package com.premnirmal.magnet.library;

import ohos.aafwk.ability.AbilityPackage;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;

public class BaseApplication extends AbilityPackage {

    public int screenWidth;
    public int screenHeight;
    public int statuBarHeight;

    private static volatile BaseApplication instance;
    private int width;
    private int height;

    public static BaseApplication getInstance() {
        BaseApplication lockInstance = instance;
        if (lockInstance == null) {
            synchronized (BaseApplication.class) {
                lockInstance = instance;
                if (lockInstance == null) {
                    lockInstance = instance = new BaseApplication();
                    instance.initScreenSzie();
                }
            }
        }
        return lockInstance;
    }

    public static void setInstance(BaseApplication instance) {
        BaseApplication.instance = instance;
    }

    @Override
    public void onInitialize() {
        super.onInitialize();
        setInstance(this);
    }

    public void initScreenSzie() {
        Display display = DisplayManager.getInstance().getDefaultDisplay(this).get();
        int width = display.getAttributes().width;
        int height = display.getAttributes().height;
        if (width < height) {
            this.width = width;
            this.height = height;
        }
    }

    public void formatScreendate() {
        Display display = DisplayManager.getInstance().getDefaultDisplay(this).get();
        int nowWidth = display.getAttributes().width;
        int nowHeight = display.getAttributes().height;
        int realHeight = display.getRealAttributes().height;
        if (nowWidth < nowHeight) {
            screenWidth = width;
            screenHeight = height;
            statuBarHeight = realHeight - nowHeight;
        } else {
            screenHeight = width;
            screenWidth = height;
        }
    }


    public int getScreenWidth() {
        formatScreendate();
        return screenWidth;
    }

    public int getScreenHeight() {
        formatScreendate();
        return screenHeight;
    }

    public int getStatuBarHeight() {
        return statuBarHeight;
    }
}
